package com.mission.schedule.activity;


import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.UserInfo;
import com.mission.schedule.bean.WBA_FindFunctionMy;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.utils.SharedPrefUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
public class WebViewActivity extends BaseActivity {

    @ViewResId(id = R.id.top_ll_left)
    private LinearLayout top_ll_left;
    @ViewResId(id = R.id.title_tv)
    private TextView title_tv;
    @ViewResId(id = R.id.webview)
    private WebView webview;
    @ViewResId(id = R.id.top_right)
    private LinearLayout top_right;

    Context context;
    String url;
    String urlId;
    String userid;
    String userName;
    String e_mail, mobile, img;
    SharedPrefUtil sharedPrefUtil = null;
    UserInfo info = null;
    String schID = "";
    String fun_id = "";
    String title = "";

    @Override
    protected void setListener() {
        top_ll_left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_webview);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");

        String remark2 = getIntent().getStringExtra("remark2");
        schID = getIntent().getStringExtra("schID");
        fun_id = getIntent().getStringExtra("fun_id");

        userid = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERID, "0");
        userName = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        e_mail = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USEREMAIL, "");
        img = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERPHOTOPATH, "");
        mobile = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.TELEPHONE, "");
        info = new UserInfo();
        info.userId = userid;
        info.nickName = userName;
        info.e_mail = e_mail;
        info.img = img;
        info.mobile = mobile;

        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);//显示放大缩小 controler
        settings.setSupportZoom(true);
        webview.getSettings().setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (TextUtils.isEmpty(remark2)) {
            webview.loadUrl(url);
            title_tv.setText(title);
        } else {
            if (remark2.equals("不提醒的解决办法") || (remark2.length() > 2 && remark2.substring(0, 2).equals("#%"))) {
                title_tv.setText(((remark2.length() > 2 && remark2.substring(0, 2).equals("#%"))) ? remark2.substring(2, remark2.length()) : remark2);
                webview.loadUrl(url);
            } else {
                WBA_FindFunctionMy.ListBean listbean = App.getDBcApplication().get_tb_function_my(remark2);
                title_tv.setText(listbean.name);
                String u = url + "?uid=" + userid + "&mobile=" + mobile + "&name=" + userName + "&email=" + e_mail + "&img=" + img;
                webview.loadUrl(u);
                top_right.setVisibility(View.VISIBLE);
            }
        }
        top_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                webview.reload();
            }
        });
//		urlId = getIntent().getStringExtra("urlId");

//		info.token = JPushInterface.getRegistrationID(getApplicationContext());
        // 启用支持javascript


//		webview.loadUrl("javascript:userInfo={"
//				+ "userId:'"+info.userId+"',"
//				+ "nickName:'"+info.nickName+"',"
//				+ "e_mail:'"+info.e_mail+"',"
//				+ "token:'"+info.token+"'}");
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        WebChromeClient wvcc = new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {

                super.onReceivedTitle(view, title);
//				title_tv.setText(title);
            }

        };
        // 设置setWebChromeClient对象

        webview.setWebChromeClient(wvcc);
    }

    @Override
    protected void setAdapter() {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        WBA_FindFunctionMy.ListBean bean = App.getDBcApplication().get_tb_function_my(fun_id == null ? "" : fun_id);
        if (schID != null && !schID.isEmpty() && bean != null && bean.remark4.equals("0")) {
            App.getDBcApplication().updateEnd(schID);
            sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.WEBEND, "1");
        }
    }
}
